package biznussoft.metercor;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ScrollView;
import biznussoft.metercor.dialogfragments.SignaturetDialogFragmen;
import biznussoft.metercor.fragments.AccountsdetailsFragment;
import biznussoft.metercor.fragments.ContactDetailsFragment;
import biznussoft.metercor.fragments.Contacts2Fragment;
import biznussoft.metercor.fragments.ERTDeviceRecordsFragment;
import biznussoft.metercor.fragments.ERTDumyFragment;
import biznussoft.metercor.fragments.JobCheckListDumyFragment;
import biznussoft.metercor.fragments.JobCheckListFragment;
import biznussoft.metercor.fragments.NewERTDeviceInstallationFragment;
import biznussoft.metercor.fragments.NewJobsiteInspectionCheckListFragmnet;
import biznussoft.metercor.fragments.NewSurveyFragment;
import biznussoft.metercor.fragments.ServiceOrder2Fragment;
import biznussoft.metercor.fragments.ServiceOrderDetailsFragment;
import biznussoft.metercor.fragments.SurveyDummyFragment;
import biznussoft.metercor.fragments.SurveyRecordsFragment;
import biznussoft.metercor.fragments.SurveySignatureFragment;
import biznussoft.metercor.listeners.ABCListener;
import biznussoft.metercor.listeners.ActivityDetailListener;
import biznussoft.metercor.listeners.DialogListener;
import biznussoft.metercor.listeners.GlobalListener;

public class AccountsActivity extends BaseActivity implements ABCListener, GlobalListener,ActivityDetailListener,DialogListener{

	ScrollView sc;
	FrameLayout fl;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.master_details_page_activity);

		sc = (ScrollView) findViewById(R.id.svTeamTimeInfo);
		fl = (FrameLayout) findViewById(R.id.viewer);
	}  

	public void onCategoryClicked(final int position) {

		sc.setVisibility(View.VISIBLE);
		fl.setVisibility(View.GONE);
		if(position >= 0){                                
			AccountsdetailsFragment newFragment0 = new AccountsdetailsFragment();
			Bundle args = new Bundle();
			args.putInt("Position", position);
			newFragment0.setArguments(args);
			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.accounts, newFragment0);
			transaction.addToBackStack(null);
			transaction.commit();
		}

		Contacts2Fragment newFragment = new Contacts2Fragment();
		FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction();
		transaction1.replace(R.id.contacts, newFragment);
		transaction1.commit();
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				Contacts2Fragment  contactFragment = (Contacts2Fragment) getSupportFragmentManager().findFragmentById(R.id.contacts);       
				contactFragment.showContacts(position);
			}
		}, 1000);

		ERTDumyFragment ertDevice = new ERTDumyFragment();
		FragmentTransaction ertTransaction = getSupportFragmentManager().beginTransaction();
		ertTransaction.replace(R.id.ertDevice, ertDevice);
		ertTransaction.commit();
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				ERTDumyFragment  contactFragment = (ERTDumyFragment) getSupportFragmentManager().findFragmentById(R.id.ertDevice);       
				contactFragment.showContacts(position);
			}
		}, 1000);

		ServiceOrder2Fragment newFragmentB = new ServiceOrder2Fragment();
		FragmentTransaction transaction2 = getSupportFragmentManager().beginTransaction();
		transaction2.replace(R.id.services, newFragmentB);
		transaction2.commit();
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				ServiceOrder2Fragment  contactFragment = (ServiceOrder2Fragment) getSupportFragmentManager().findFragmentById(R.id.services);       
				contactFragment.showServiceOrders(position);
			}
		}, 1000);

		JobCheckListDumyFragment newFragmentc = new JobCheckListDumyFragment();
		FragmentTransaction transaction3 = getSupportFragmentManager().beginTransaction();
		transaction3.replace(R.id.jobs, newFragmentc);
		transaction3.commit();
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {

				JobCheckListDumyFragment  contactFragment = (JobCheckListDumyFragment) getSupportFragmentManager().findFragmentById(R.id.jobs);       
				contactFragment.showJobCheckList(position);
			}
		}, 1000);

		SurveyDummyFragment survey = new SurveyDummyFragment();
		FragmentTransaction surveyTransaction = getSupportFragmentManager().beginTransaction();
		surveyTransaction.replace(R.id.survey, survey);
		surveyTransaction.commit();
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {

				SurveyDummyFragment  contactFragment = (SurveyDummyFragment) getSupportFragmentManager().findFragmentById(R.id.survey);       
				contactFragment.showJobCheckList(position);
			}
		}, 1000);

	}

	@Override
	public void onNewContact(String[] args) {}

	@Override
	public void onGetContacts(final int position,final String category) {

		sc.setVisibility(View.GONE);
		fl.setVisibility(View.VISIBLE);
		ContactDetailsFragment newFragment = new ContactDetailsFragment();
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.replace(R.id.viewer, newFragment);
		transaction.addToBackStack(null);
		transaction.commit();

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				ContactDetailsFragment  contactFragment = (ContactDetailsFragment) getSupportFragmentManager().findFragmentById(R.id.viewer);       
				contactFragment.showContacts(position,category);
			}
		}, 1000);

	}


	@Override
	public void getSignature(String base64) {
		sc.setVisibility(View.GONE);
		fl.setVisibility(View.VISIBLE);
		if(base64.equals("ERT")){
			NewERTDeviceInstallationFragment newFragmentc = new NewERTDeviceInstallationFragment();
			FragmentTransaction transaction3 = getSupportFragmentManager().beginTransaction();
			transaction3.replace(R.id.viewer, newFragmentc);
			transaction3.commit();}
		if(base64.equals("Jobsite")){
			NewJobsiteInspectionCheckListFragmnet newFragmentc = new NewJobsiteInspectionCheckListFragmnet();
			FragmentTransaction transaction3 = getSupportFragmentManager().beginTransaction();
			transaction3.replace(R.id.viewer, newFragmentc);
			transaction3.commit();
		}
		if(base64.equals("Survey")){
			NewSurveyFragment newFragmentc = new NewSurveyFragment();
			FragmentTransaction transaction3 = getSupportFragmentManager().beginTransaction();
			transaction3.replace(R.id.viewer, newFragmentc);
			transaction3.commit();
		}
		if(base64.equals("ERTDevice")){
			NewERTDeviceInstallationFragment newFragmentc = new NewERTDeviceInstallationFragment();
			FragmentTransaction transaction3 = getSupportFragmentManager().beginTransaction();
			transaction3.replace(R.id.viewer, newFragmentc);
			transaction3.commit();
		}
		if(base64.equals("JobSiteFragment")){
			JobCheckListFragment newFragmentc = new JobCheckListFragment();
			FragmentTransaction transaction3 = getSupportFragmentManager().beginTransaction();
			transaction3.replace(R.id.viewer, newFragmentc);
			transaction3.commit();
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {

					JobCheckListFragment  contactFragment = (JobCheckListFragment) getSupportFragmentManager().findFragmentById(R.id.viewer);       
					contactFragment.showJobCheckList();
				}
			}, 1000);
		}
		if(base64.equals("ERTDeviceRecords")){
			ERTDeviceRecordsFragment newFragmentc = new ERTDeviceRecordsFragment();
			FragmentTransaction transaction3 = getSupportFragmentManager().beginTransaction();
			transaction3.replace(R.id.viewer, newFragmentc);
			transaction3.commit();
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {

					ERTDeviceRecordsFragment  contactFragment = (ERTDeviceRecordsFragment) getSupportFragmentManager().findFragmentById(R.id.viewer);       
					contactFragment.showJobCheckList();
				}
			}, 1000);
		}

		if(base64.equals("SurveyFragment")){
			SurveyRecordsFragment newFragmentc = new SurveyRecordsFragment();
			FragmentTransaction transaction3 = getSupportFragmentManager().beginTransaction();
			transaction3.replace(R.id.viewer, newFragmentc);
			transaction3.commit();
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {

					SurveyRecordsFragment  contactFragment = (SurveyRecordsFragment) getSupportFragmentManager().findFragmentById(R.id.viewer);       
					contactFragment.showJobCheckList();
				}
			}, 1000);
		}

	}

	@Override
	public void displayFields(int position, String category) {

	}

	@Override
	public void onServiceClicked(final int position) {
		// TODO Auto-generated method stub
		sc.setVisibility(View.GONE);
		fl.setVisibility(View.VISIBLE);
		ServiceOrderDetailsFragment newFragment = new ServiceOrderDetailsFragment();
		FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
		transaction.replace(R.id.viewer, newFragment);
		transaction.addToBackStack(null);
		transaction.commit();
		new Handler().postDelayed(new Runnable() {                      
			@Override
			public void run() {
				ServiceOrderDetailsFragment warehouseFragment = (ServiceOrderDetailsFragment) getSupportFragmentManager().findFragmentById(R.id.viewer);
				warehouseFragment.getWarehouseDetails(position,"accounts");
			}
		}, 1000);
	}

	@Override
	public void getCategory(String category, String id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void nextDialog(String type) {
		// TODO Auto-generated method stub
		if(type.equalsIgnoreCase("sign")){
			SurveySignatureFragment.newInstace().show(getSupportFragmentManager(), "dialog");
		}
	}

	@Override
	public void setDateTime(StringBuffer sb) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void getSignature2(String base64) {
		// TODO Auto-generated method stub
		NewSurveyFragment warehouseFragment = (NewSurveyFragment) getSupportFragmentManager().findFragmentById(R.id.viewer);
		warehouseFragment.getSignature(base64);
	}


}
